(function(window){
	var console={
	windoLimit:12,   	//允许打开窗口个数
	windowNum:0,   	//打开窗口个数
	tabWidth:700,		//tab区域
	singleTab:121,		//单个tab宽度
	moveWidth:50,		//单次移动宽度
	moduleCode:null,
	initEvent:function(){
		//关闭所有tab
		$("#closeAll").click(Console.closeAllTabPage);
		
		// 模块选择下拉
		$("#curModule").click(function(){
			if($(this).hasClass('current')){
				$(this).removeClass('current');
				$("#moduleList").hide();
			}else{
				$(this).addClass('current');
				$("#moduleList").show();
			}
		});
		$("#moduleList a").click(function(){
			$("#curModule").html($(this).html()).removeClass('current');
			$("#moduleList").hide();
			var code = $(this).attr("code");
			if(code == Console.moduleCode) return;
			Console.moduleCode = code;
			Console.loadNode(code);	
		});
		$(".top_module").mouseleave(function(){
			$("#curModule").removeClass('current');;
			$("#moduleList").hide();
		});
		
		//退出系统事件
		$("#exitBtn").click(function(){
			if(!window.confirm("您确定要退出系统吗！")){
				return;
			}
			location.href = "loginAction.do?method=logout";
		});
		
		//默认首页tab点击事件
		$("#menuId_0").click(function(){
			var curMenuId=$("#menuTab .current").attr("code");
			if(curMenuId == "0"){
				return;
			}
			Console.unActiveTabPage(curMenuId);
			Console.activeTabPage("0");
		});
			
		//计算tab总的宽度
		$(window).resize(function(){
			Console.tabWidth = $("#menuTabPart").width();
			Console.renderTabStyle();
		});
		$(window).trigger("resize");
		
		//向左向右滚动
		$("#tabPrev").click(Console.turnRightTab);
		$("#tabNext").click(Console.turnLeftTab);
	},
	//移动tab
	scrollMenuTab:function(moveWidth){
		$("#menuTab").stop(true,false).animate({"left":-moveWidth},500);
	},
	
	//渲染tab移动样式
	renderTabStyle:function(){
		var width = Console.windowNum*Console.singleTab - Console.tabWidth;
		if(width<=0){
			Console.scrollMenuTab(0);
			$("#tabPrev,#tabNext").hide();
			return;
		}
		$("#tabPrev").show();
		$("#tabNext").hide();
		Console.scrollMenuTab(width);
	},
	turnRightTab:function(){
		var leftStr = $("#menuTab").css("left").replace("px", "");
		var left = parseInt(leftStr);
		if(left>=0) {
			$("#tabPrev").hide();
			Console.scrollMenuTab(0);
			return;
		}
		var width =  Math.abs(left) - Console.moveWidth;
		$("#tabNext").show();
		if(width>0){
			$("#tabPrev").show();
		}else{
			$("#tabPrev").hide();
		}
		Console.scrollMenuTab(width>0? width : 0);
	},
	turnLeftTab:function(){
		var leftStr = $("#menuTab").css("left").replace("px", "");
		var left = Math.abs(parseInt(leftStr));
		var width = left + Console.moveWidth;
		$("#tabPrev").show();
		var diffWidth = Console.windowNum*Console.singleTab-Console.tabWidth;
		Console.scrollMenuTab(diffWidth>width? width : diffWidth);
		if(diffWidth>width){
			$("#tabNext").show();
		}else{
			$("#tabNext").hide();
		}
	},
	//展开子菜单
	expandMenu:function(){
		$(".sub-menu").show();
	},
	//隐藏子菜单
	collapseMenu:function(){
		$(".sub-menu").hide();
	},
	//展开子菜单
	showMenu:function(tbnum){
		var curEl = $("#menuBar"+tbnum);
		if (curEl.is(":hidden")){
			curEl.show();
		}else{
			curEl.hide();
		}
	},
	
	navPage:function(el, rightCode, url){
		Console.openTagPage(rightCode, $(el).html(), url);
	},
	//打开新的tab页面
	openTagPage:function(rightCode, menuTitle, url){
		var menuEl = $("#menuId_"+rightCode);
		var curMenuId=$("#menuTab .current").attr("code");
		if(!curMenuId || JFT.isBlank(curMenuId)){
			curMenuId = "0";
		}
		if(menuEl && menuEl.length){
			if(rightCode==curMenuId) return;
			Console.unActiveTabPage(curMenuId);
			Console.activeTabPage(rightCode);
			return;	
		}
		url = !url? "main.html" : url;
		if(!Console.createTabPage(rightCode, url , menuTitle)){
			return;	
		}
		Console.unActiveTabPage(curMenuId);
		Console.activeTabPage(rightCode);
		Console.renderTabStyle();
	},
	//关闭的tab页面
	shutTabPage:function(code){
		$("#menuId_"+code).trigger("dblclick");
	},
	//钝化tab及对应页
	unActiveTabPage:function(code){
		$("#mainframe"+code).hide();
		$("#menuId_"+code).removeClass("current");	
	},
	//激活tab及对应页
	activeTabPage:function(code){
		$("#mainframe"+code).show();
		$("#menuId_"+code).addClass("current");	
	},
	//关闭tab及对应页
	closeTabPage:function(code){
		$("#mainframe"+code).remove();
		var menuEl = $("#menuId_"+code);
		menuEl.unbind();
		menuEl.find(".close").unbind();
		menuEl.remove();
		Console.windowNum--;
		Console.renderTabStyle();
	},
	//关闭所有页面
	closeAllTabPage:function(){
		//连续关闭tab
		$("#menuTab dd").each(function(){
			Console.closeTabPage($(this).attr("code"));
		});
		$("#tabPrev,#tabNext").hide();
		//激活首页
		Console.activeTabPage("0");
	},
	//生成tab及对应页面
	createTabPage:function(code, url, title){
		if(Console.windowNum>=Console.windoLimit){
			alert("最多只能同时打开"+Console.windoLimit+"个窗口！");
			return false;	
		}
		var strLi=[];
		strLi.push('<dd id="menuId_'+code+'" code="'+code+'">');
		strLi.push('<div class="close" title="关闭"></div>');
		strLi.push('<a href="javascript:void(0)">'+title+'</a>');
		strLi.push('</dd>');
		
		url += "&RIGHT_CODE="+code;
		//创建元素
		$("#menuTab").append(strLi.join(""));
		$("#mainArea").append('<iframe id="mainframe'+code+'" name="mainframe'+code+'" frameborder="0" src="'+url+'" style="display:none;"></iframe>');
		
		//绑定事件
		Console.bindTabEvent(code);
		
		strLi=null;
		Console.windowNum++;
		return true;
	},
	chkCloseTabPage:function(tabEl){
		if(tabEl.hasClass("current")){
			var tmpEl = tabEl.next();
			if(!tmpEl || !tmpEl.length){
				tmpEl = tabEl.prev();	
			}
			Console.activeTabPage(tmpEl.attr("code"));
		}
		var code=tabEl.attr("code");
		Console.closeTabPage(code);
	},
	//绑定tab事件
	bindTabEvent:function(code){
		var menuEl = $("#menuId_"+code);
		menuEl.bind("mouseover", function(){
			$(this).find(".close").show();
		}).bind("mouseleave", function(){
			$(this).find(".close").hide();
		}).bind("click", function(){
			var rightCode = $(this).attr("code");
			var curMenuId=$("#menuTab .current").attr("code");
			if(!curMenuId || JFT.isBlank(curMenuId)){
				curMenuId = "0";
			}
			if(curMenuId == rightCode){
				return;	
			}
			Console.unActiveTabPage(curMenuId);
			Console.activeTabPage(rightCode);
		}).bind("dblclick", function(){
			Console.chkCloseTabPage(menuEl);
		}); 
		menuEl.find(".close").bind("click", function(){
			Console.chkCloseTabPage($(this).parent());
		});
	},
	nodeData:{},
	menuData:{},
	loadNode:function(moduleCode){
		if(Console.nodeData[moduleCode]){
			Console.renderNode(Console.nodeData[moduleCode]);
			return;
		}
		beginPageLoading();
		ajaxPost("adminAction.do?method=loadNode", "moduleCode="+moduleCode, function(data){
			endPageLoading();
			if(data["resultCode"] != 0){
				alert("数据加载异常");
				return;
			}
			Console.nodeData[moduleCode]=data["resultData"];
			Console.renderNode(data["resultData"]);
		});
	},
	renderNode:function(nodeList){
		var nodeHtml = [];
		var firstNode = null;
		$.each(nodeList, function(i, node){
			nodeHtml.push('<li>');
			nodeHtml.push('<a id="menuBar_'+node['CODE']+'" name="menuBar" code="'+node["CODE"]+'" href="javascript:void(0)">');
			nodeHtml.push('<img src="img/sub_menu_icon1.png"/>');
			nodeHtml.push('<span>'+node["NAME"]+'</span>');
			nodeHtml.push('</a>');
			nodeHtml.push('<dl></dl>');
			nodeHtml.push('</li>');
			if(i==0){
				firstNode = node['CODE'];
			}
		});
		//解除之前事件绑定
		$("#menuPart").find("a[name=menuBar]").unbind("click");
		$("#menuPart ul").html(nodeHtml.join(""));
		$("#menuPart").find("a[name=menuBar]").bind("click", Console.clickNode);
		//打开第一个菜单
		$("#menuBar_"+firstNode).trigger("click");
	},
	clickNode:function(){
		var nodeCode = $(this).attr("code");
		if(Console.menuData[nodeCode]){
			Console.renderMenu(nodeCode);
			return;
		}
		beginPageLoading();
		ajaxPost("adminAction.do?method=loadMenu", "nodeCode="+nodeCode, function(data){
			endPageLoading();
			if(data["resultCode"] != 0){
				alert("数据加载异常");
				return;
			}
			Console.menuData[nodeCode]=data["resultData"];
			Console.renderMenu(nodeCode);
		});
	},
	renderMenu:function(nodeCode){
		var nodeEl = $("#menuBar_"+nodeCode);
		var menuPart = nodeEl.next("dl");
		var menuEl = menuPart.find("dd");
		if(!menuEl || !menuEl.length){
			var menuHtml = [];
			var menuList = Console.menuData[nodeCode];
			$.each(menuList, function(i, menu){
				menuHtml.push('<dd><a href="javascript:void(0)" onclick="Console.navPage(this, \''+menu["RIGHT_CODE"]+'\', \''+menu["MENU_URL"]+'\')">'+menu["MENU_NAME"]+'</a></dd>');
			});
			menuPart.html(menuHtml.join(""));
		}
		menuPart.stop(true,true).slideToggle();
		nodeEl.parent().toggleClass('current');
	}
	};
	window.Console=console;
})(window);

$(function(){
	//加载菜单模块
	var code = $("#curModule").attr("code");
	if(code && !JFT.isBlank(code)){
		Console.loadNode(code);	
		Console.moduleCode = code;
	}
	
	Console.initEvent();
	
});


	